package com.socool.sinofaith.service.updater;

import java.io.File;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.socool.sinofaith.R;
import com.socool.sinofaith.service.updater.DownloadService.DownloadServiceListener;
 

public class DownloadActivity extends Activity {

	private static final String TAG = "DownloadActivity";

	private DownloadService mService;

	private String downloadFilePath;

	private ProgressBar mProgress;

	private TextView mTitleView;

	private TextView mPercentView;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.download_main);
		Intent intent = getIntent();
		String model = intent.getStringExtra("model");
		String branch = intent.getStringExtra("branch");
		String date = intent.getStringExtra("date");

		downloadFilePath = intent.getStringExtra("localPath");

		final Intent service = new Intent(DownloadActivity.this,
				DownloadService.class);
		bindService(service, mConnection, Context.BIND_AUTO_CREATE);

		TextView title = (TextView) findViewById(R.id.download_message);
//		title.setText("升级包文件下载中..."+date + "-"
//				+ model + "-" + branch);
		title.setText("升级包文件下载中...");

		mProgress = (ProgressBar) findViewById(R.id.progress);
		mTitleView = (TextView) findViewById(R.id.title);
		mPercentView = (TextView) findViewById(R.id.percent);

		findViewById(R.id.cancel).setOnClickListener(
				new View.OnClickListener() {

					public void onClick(View v) {
						if (mService != null) {
							mService.cancel();

							try {
								File file = new File(downloadFilePath);
								if (file.exists()) {
									file.delete();
								}
							} catch (Exception e) {
//								e.printStackTrace();
							}
							finish();
						}
					}
				});

		mTitleView.setText(date);

	}

	public void onDestroy() {
		unbindService(mConnection);
		if (mService != null) {
			mService.setListener(null);
			mService = null;
		}
		super.onDestroy();
	}

	private ServiceConnection mConnection = new ServiceConnection() {

		public void onServiceConnected(ComponentName className, IBinder service) {
//			if (CarWizardSetting.DEBUG)
//				Log.d(TAG, "=onServiceConnected");
			mService = ((DownloadService.LocalBinder) service).getService();
			mService.setListener(mListener);

		}

		public void onServiceDisconnected(ComponentName className) {
//			if (CarWizardSetting.DEBUG)
//				Log.d(TAG, "=onServiceDisconnected");
			if (mService != null) {
				mService.setListener(null);
				mService = null;
			}
		}
	};

	private DownloadServiceListener mListener = new DownloadServiceListener() {

		public void onDownloadPercent(final int percent) {

//			if (CarWizardSetting.DEBUG)
//				Log.d(TAG, "= activity download percent");
			runOnUiThread(new Runnable() {
				public void run() {
					mProgress.setProgress(percent);
					mPercentView.setText(percent + "%");
				}
			});
		}

		public void onDownloadFinish() {//下载完成之后自动安装

//			if (CarWizardSetting.DEBUG)
//				Log.d(TAG, "= activity download finish");
			runOnUiThread(new Runnable() {
				public void run() {

					AutoInstall.setUrl(downloadFilePath);//安装文件的路径
					AutoInstall.install(DownloadActivity.this);
					finish();
				}
			});
		}
	};

}
